from rest_framework import serializers
from myapp.models import *



class ProcessSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    approver = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    approver_name = serializers.SerializerMethodField()
    department = serializers.PrimaryKeyRelatedField(queryset=DepartmentModel.objects.all())
    department_name = serializers.SerializerMethodField()
    apply = serializers.PrimaryKeyRelatedField(queryset=ApplyTypeModel.objects.all())
    apply_name = serializers.SerializerMethodField()
    def get_user_name(self,obj):
        return obj.user.username

    def get_approver_name(self,obj):
        return obj.approver.username

    def get_department_name(self,obj):
        return obj.department.d_name

    def get_apply_name(self,obj):
        return obj.apply.name

    class Meta:
        model = ProcessInsModel
        fields = '__all__'



# 申请实例信息序列化器
class ProcessInsSer(serializers.ModelSerializer):
    username = serializers.SerializerMethodField(read_only=True)
    apply = serializers.SerializerMethodField(read_only=True)
    apply_type = serializers.SerializerMethodField(read_only=True)
    apply_cate = serializers.SerializerMethodField(read_only=True)
    approver = serializers.SerializerMethodField(read_only=True)
    department = serializers.SerializerMethodField(read_only=True)
    state_name = serializers.SerializerMethodField(read_only=True)


    class Meta:
        model = ProcessInsModel
        exclude = ('created_time', 'updated_time', 'logic_del', 'user',)
        # exclude = ('password', 'department', 'rank', 'jobs', 'role')
        # exclude 和 fields 不可以同时使用

    def get_username(self, obj):
        return obj.user.username
    def get_apply(self, obj):
        return obj.apply.apply.id
    def get_apply_type(self, obj):
        return obj.apply.apply.name
    def get_apply_cate(self, obj):
        return obj.apply.leaves_type
    def get_approver(self, obj):
        return obj.approver.username
    def get_department(self, obj):
        return obj.user.department.d_name
    def get_state_name(self, obj):
        if obj.state == 0:
            return '未审批'
        elif obj.state ==1:
            return '审批中'
        elif obj.state ==2:
            return '已通过'
        elif obj.state ==3:
            return '已驳回'
        elif obj.state ==4:
            return '已撤回'



# 申请分类序列化器
class ApplyCategorySer(serializers.ModelSerializer):

    class Meta:
        model = ApplyCategoryModel
        exclude = ('created_time', 'updated_time', 'logic_del', 'apply')




class ApproverSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    username = serializers.SerializerMethodField()

    def get_username(self,obj):
        return obj.user.username
    class Meta:
        model =ApproverModel
        fields = '__all__'